package cn.message.web;

import cn.itcast.pojo.horseman;
import cn.itcast.pojo.merchant;
import cn.itcast.util.OutputStringController;
import cn.message.service.IMerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-05-26 9:30
 */
@Slf4j
@RestController("merchantController_message")
@RequestMapping(value="/dhn/message",produces="text/html;charset=UTF-8")
public class MerchantController {

    @Resource(name = "merchantService_message")
    private IMerchantService merchantService;

    @Resource
    private OutputStringController osc;

    /**
     * 返回所有已开通的商家列表信息
     * @return
     */
    @PostMapping("/getAllOpenMerchant")
    @ResponseBody
    public String getAllOpenMerchant(){

        String results = "";
        List<merchant> merchantList = merchantService.getAllOpenMerchant();

        if(merchantList == null){
            results = osc.failure("查询所有商家信息失败");
        }
        else {
            results = osc.resultSuccess("查询所欲商家信息成功",merchantList);
        }

        return results;
    }

    /**
     * 返回所有商家列表信息
     * @return
     */
    @PostMapping("/getAllMerchant")
    @ResponseBody
    public String getAllMerchant(@RequestBody Map<String,String> map){

        String results = "";
        Map<String, Object> resultMap = merchantService.getAllMerchant(map);
        //获取所有商家列表信息
        Object merchantList = resultMap.get("merchantList");
        //获取符合查询条件的总记录条数
        int count = (int)resultMap.get("count");

        if(merchantList == null){
            results = osc.failure("查询所有商家信息失败");
        }
        else {
            results = osc.resultSuccess("查询所欲商家信息成功",merchantList, count);
        }

        return results;
    }

    /**
     * 根据商家id删除商家
     * @param map
     * @return
     */
    @RequestMapping("/deleteMerchant")
    @ResponseBody
    public String deleteMerchant(@RequestBody Map<String,String> map){
        String results = "";
        int merchantId = Integer.parseInt(map.get("merchantId"));
        Boolean aBoolean = merchantService.deleteMerchantByMerchantId(merchantId);
        if(aBoolean = false){
            results = osc.failure("系统异常，删除失败");
        }
        else {
            results = osc.success("删除成功");
        }
        return results;
    }

    /**
     * 增加商家主账号
     * @param map
     * @return
     */
    @RequestMapping("/addMainMerchant")
    @ResponseBody
    public String addMainMerchant(@RequestBody Map<String,String> map){
        String results="";

        if(map.get("merchantNumber").isEmpty()){
            return osc.failure("用户名不能为空");
        }
        if(map.get("merchantPhone").isEmpty()){
            return osc.failure("商家手机号不能为空");
        }
        if(map.get("merchantPassword").isEmpty()){
            return osc.failure("密码不能为空");
        }
        if(map.get("merchantName").isEmpty()){
            return osc.failure("商家姓名不能为空");
        }
        merchant merchant = new merchant();
        merchant.setCreateTime(new Date());
        merchant.setMerchantName(map.get("merchantName"));
        merchant.setMerchantPhone(map.get("merchantPhone"));
        merchant.setMerchantPassword(map.get("merchantPassword"));
        merchant.setMerchantNumber(map.get("merchantNumber"));
        merchant.setMerchantFid(0);
        merchant.setMerchantIsopen("0");
        boolean aBoolean = merchantService.addMerchant(merchant);
        if(!aBoolean){
            results = osc.failure("系统异常，增加商家主账户失败");
        }
        else {
            results = osc.success("增加商家主账户成功");
        }

        return results;
    }

    /**
     * 增加商家子账号
     * @param map
     * @return
     */
    @RequestMapping("/addChildrenMerchant")
    @ResponseBody
    public String addChildrenMerchant(@RequestBody Map<String,String> map){
        String results="";

        if(map.get("merchantNumber").isEmpty()){
            return osc.failure("用户名不能为空");
        }
        if(map.get("merchantPhone").isEmpty()){
            return osc.failure("商家手机号不能为空");
        }
        if(map.get("merchantPassword").isEmpty()){
            return osc.failure("密码不能为空");
        }
        if(map.get("merchantName").isEmpty()){
            return osc.failure("商家姓名不能为空");
        }
        if(map.get("merchantFid").isEmpty()){
            return osc.failure("请选择主账号");
        }
        merchant merchant = new merchant();
        merchant.setCreateTime(new Date());
        merchant.setMerchantName(map.get("merchantName"));
        merchant.setMerchantPhone(map.get("merchantPhone"));
        merchant.setMerchantPassword(map.get("merchantPassword"));
        merchant.setMerchantNumber(map.get("merchantNumber"));
        merchant.setMerchantFid(Integer.parseInt(map.get("merchantFid")));
        boolean aBoolean = merchantService.addMerchant(merchant);
        if(!aBoolean){
            results = osc.failure("系统异常，增加商家子账户失败");
        }
        else {
            results = osc.success("增加商家子账户成功");
        }

        return results;
    }

    /**
     * 修改商家信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateMerchant")
    public String updateMerchant(@RequestBody Map<String,String> map){

        String results = "";
        horseman horseman;
        if(map.get("merchantNumber").isEmpty()){
            return osc.failure("用户名不能为空");
        }
        if(map.get("merchantPhone").isEmpty()){
            return osc.failure("商家手机号不能为空");
        }
        if(map.get("merchantPassword").isEmpty()){
            return osc.failure("密码不能为空");
        }
        if(map.get("merchantName").isEmpty()){
            return osc.failure("商家姓名不能为空");
        }
        if(map.get("merchantFid").isEmpty()){
            return osc.failure("请选择主账号");
        }
        merchant merchant = new merchant();
        merchant.setMerchantId(Integer.parseInt(map.get("merchantId")));
        merchant.setCreateTime(new Date());
        merchant.setMerchantName(map.get("merchantName"));
        merchant.setMerchantPhone(map.get("merchantPhone"));
        merchant.setMerchantPassword(map.get("merchantPassword"));
        merchant.setMerchantNumber(map.get("merchantNumber"));
        merchant.setMerchantFid(Integer.parseInt(map.get("merchantFid")));
        boolean aBoolean = merchantService.updateMerchant(merchant);
        if(aBoolean == false){
            results = osc.failure("系统异常，修改失败");
        }
        else {
            results = osc.success("修改成功");
        }
        return results;
    }

    /**
     * 开通和关闭商家
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/openAndCloseMerchant")
    public String openAndCloseMerchant(@RequestBody Map<String,String> map){
        log.debug(map.get("merchantIsopen"));
        log.debug(map.get("merchantId"));
        String results = "";
        merchant merchant = new merchant();
        merchant.setMerchantIsopen(map.get("merchantIsopen"));
        merchant.setMerchantId(Integer.parseInt(map.get("merchantId")));
        Boolean aBoolean = merchantService.updateMerchant(merchant);
        String msg = "";
        if(aBoolean == false){
            if(Integer.parseInt(map.get("merchantIsopen")) == 1){
                msg = "系统异常，开通失败";
            }else {
                msg = "系统异常，关闭失败";
            }
            results = osc.failure(msg);
        }
        else {
            if(Integer.parseInt(map.get("merchantIsopen")) == 1){
                msg = "开通成功";
            }else {
                msg = "关闭成功";
            }
            results = osc.success(msg);
        }

        return results;
    }

}
